﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.DataAccess;

namespace Kiiro.Model
{
    public class CustomModel
    {
        public static SPContext GetCustomFieldsContext (  
            Type type, 
            int itemId, 
            HttpContext httpContext, 
            Guid siteCollectionId, 
            string serverRelativeUrl )
        {
            return CustomDA.Instance.GetCustomFieldsContext ( type, itemId, httpContext, siteCollectionId, serverRelativeUrl );
        }

        public static List<string> GetCustomContentTypeNames ( Guid siteCollectionId, string serverRelativeUrl )
        {
            return CustomDA.Instance.GetCustomContentTypeNames ( siteCollectionId, serverRelativeUrl );
        }

        public static List<CustomItem> GetAllCustomItems ( List<CustomItem> customItems, Guid siteCollectionId, string serverRelativeUrl )
        {
            return CustomDA.Instance.GetAllCustomItems ( customItems, siteCollectionId, serverRelativeUrl );
        }

        public static Dictionary<string, string> HarvestCustomFields(Type type,
            int itemId,
            HttpContext httpContext,
            Guid siteCollectionId,
            string serverRelativeUrl)
        {
            SPContext context = GetCustomFieldsContext(type, itemId, httpContext, siteCollectionId, serverRelativeUrl);

            Dictionary<string, string> customFields = new Dictionary<string, string>();

            List<SPField> fields = new List<SPField>();
            foreach (SPField field in context.List.Fields)
            {
                if (field.Group == "Kiiro Custom Columns")
                    fields.Add(field);
            }

            foreach (SPField field in fields)
            {
                string fieldTitle = field.Title;
                string fieldValue = null;
                if (context.Item[fieldTitle] != null)
                    fieldValue = context.Item[fieldTitle].ToString();

                customFields.Add(fieldTitle, fieldValue);
            }

            return customFields;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/CustomModel.cs $
 * 
 * 4     12/07/09 3:16p Stefanf
 * 
 * 3     17/08/09 10:29a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/